//
//  HYLabel.m
//  HaiYue
//
//  Created by skyline on 2017/12/19.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYLabel.h"
@interface HYLabel()
@end
@implementation HYLabel
- (instancetype)initWithText:(NSString *)text textColor:(UIColor *)textColor
{
    self = [super init];
    if (self)
    {
        self.fixedWidth = 0;
        self.text = text;
        self.textColor = textColor;
    }
    return self;
}

- (instancetype)initWithText:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)font
{
    self = [super init];
    if (self)
    {
        self.fixedWidth = 0;
        self.font = font;
        self.text = text;
        self.textColor = textColor;
    }
    return self;
}

- (CGFloat)HeightLine
{
    CGRect rect = [@"标准字" getHeightWithWidth:_fixedWidth withFont:self.font];
    return ceil(CGRectGetHeight(rect));
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    if (_fixedWidth  > 0)
    {
        //
        self.numberOfLines = (self.numberOfLines == 1)?0:self.numberOfLines;
        //
        //        NSString *string = (self.numberOfLines == 0)?text:[text substringToIndex:3];
        //        CGRect rect = [string getHeightWithWidth:_fixedWidth withFont:self.font];
        //        CGFloat height = (self.numberOfLines == 0)?ceil(rect.size.height):ceil(rect.size.height * 1.0 * self.numberOfLines);
        //        
        //        self.bounds = CGRectMake(0, 0, _fixedWidth, height);
        //        [self mas_updateConstraints:^(MASConstraintMaker *make) {
        //            make.size.mas_equalTo(self.bounds.size);
        //        }];
    }
    else
    {
        [self sizeToFit];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.bounds.size);
        }];
    }
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    if (_fixedWidth  > 0)
    {
        self.numberOfLines = 0;
        //        CGRect rect = [attributedText.string getHeightWithWidth:_fixedWidth withFont:self.font];
        //        self.bounds = CGRectMake(0, 0, _fixedWidth, ceil(rect.size.height));
        //        [self mas_updateConstraints:^(MASConstraintMaker *make) {
        //            make.size.mas_equalTo(self.bounds.size);
        //        }];
    }
    else
    {
        [self sizeToFit];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.bounds.size);
        }];
    }
}

- (void)setNumberOfLines:(NSInteger)numberOfLines
{
    [super setNumberOfLines:numberOfLines];
    if (numberOfLines == 0 && self.fixedWidth > 0)
    {
        NSString *text = ([self.attributedText.string length] > 0)?self.attributedText.string:self.text;
        CGRect rect = [text getHeightWithWidth:self.fixedWidth withFont:self.font];
        self.bounds = CGRectMake(0, 0, self.fixedWidth, ceil(rect.size.height));
    }
    else
    {
        if (self.fixedWidth > 0)
        {
            self.bounds = CGRectMake(0, 0, self.fixedWidth, self.HeightLine * numberOfLines);
        }
        else
        {
            [self sizeToFit];
            self.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        }
    }
    
}

- (void)setBounds:(CGRect)bounds
{
    [super setBounds:bounds];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.bounds.size);
    }];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
